package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.constant.WareConstant;
import com.atguigu.gulimall.ware.entity.WmsPurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.WmsPurchaseDetailService;
import com.atguigu.gulimall.ware.service.WmsWareSkuService;
import com.atguigu.gulimall.ware.vo.MergeVo;
import com.atguigu.gulimall.ware.vo.PurchaseDoneVo;
import com.atguigu.gulimall.ware.vo.PurchaseItemDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WmsPurchaseDao;
import com.atguigu.gulimall.ware.entity.WmsPurchaseEntity;
import com.atguigu.gulimall.ware.service.WmsPurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("wmsPurchaseService")
public class WmsPurchaseServiceImpl extends ServiceImpl<WmsPurchaseDao, WmsPurchaseEntity> implements WmsPurchaseService {
    @Autowired
    WmsPurchaseDetailService purchaseDetailService;
    @Autowired
    WmsWareSkuService wareSkuService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WmsPurchaseEntity> wrapper = new QueryWrapper<>();


        IPage<WmsPurchaseEntity> page = this.page(
                new Query<WmsPurchaseEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        QueryWrapper<WmsPurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0).or().eq("status",1);

        IPage<WmsPurchaseEntity> page = this.page(
                new Query<WmsPurchaseEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void mergeList(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();

        if (purchaseId == null){
            WmsPurchaseEntity purchaseEntity = new WmsPurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
        }
        List<Long> items = mergeVo.getItems();
        List<WmsPurchaseDetailEntity> collect = items.stream().map(i -> {
            WmsPurchaseDetailEntity purchaseDetailEntity = new WmsPurchaseDetailEntity();
            purchaseDetailEntity.setId(i);
            purchaseDetailEntity.setPurchaseId(purchaseId);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
        WmsPurchaseEntity purchaseEntity = new WmsPurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }
    @Transactional
    @Override
    public void received(List<Long> ids) {
        List<WmsPurchaseEntity> collect = ids.stream().map(id -> {
            WmsPurchaseEntity purchaseEntity = this.getById(id);
            return purchaseEntity;
        }).filter(entity -> {
            if (entity.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() ||
                    entity.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()) {
                entity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
                entity.setUpdateTime(new Date());
                return true;
            } else
                return false;
        }).collect(Collectors.toList());
        this.updateBatchById(collect);
        //修改采购需求状态
        collect.forEach(item->{
            List<WmsPurchaseDetailEntity> purchaseDetailEntitys = purchaseDetailService.list(new QueryWrapper<WmsPurchaseDetailEntity>().eq("purchase_id",item.getId()));
            List<WmsPurchaseDetailEntity> detailEntityList = purchaseDetailEntitys.stream().map(detail -> {
                detail.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return detail;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(purchaseDetailEntitys);
        });

    }

    @Transactional
    @Override
    public void done(PurchaseDoneVo vo) {
        //采购单id
        WmsPurchaseEntity purchaseEntity = this.getById(vo.getId());
        AtomicBoolean flag = new AtomicBoolean(true);
        //采购需求id
        List<PurchaseItemDoneVo> items = vo.getItems();
        //遍历采购需求id生成需求实体类列表
        List<WmsPurchaseDetailEntity> details = items.stream().map(item -> {
            WmsPurchaseDetailEntity detailEntity = purchaseDetailService.getById(item.getItemId());
            detailEntity.setStatus(item.getStatus());
            if (item.getStatus() == WareConstant.PurchaseDetailStatusEnum.FINISH.getCode()){
                flag.set(true);
                //sku_id  ware_id   stock  sku_name      stock_locked
                wareSkuService.addStock(detailEntity.getSkuId(),detailEntity.getWareId(),detailEntity.getSkuNum());
            }else flag.set(false);
            return detailEntity;
        }).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(details);
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setStatus(flag.get()?WareConstant.PurchaseStatusEnum.FINISH.getCode():WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        this.updateById(purchaseEntity);
    }

}